﻿using System;
using System.Drawing;
using System.IO;

namespace MapCenterSystem.ServiceProvider
{
    public abstract class ServiceMapProvider
    {
        protected string mapStore;
        protected int zoom;
        protected Size size;

        public int Zoom
        {
            get { return zoom; }
            set { zoom = value; }
        }

        public Size Size
        {
            get { return size; }
            set { size = value; }
        }
            

        public virtual string MapStore
        {
            get { return mapStore; }
            set { mapStore = value; }
        }

        
        public virtual byte[] GetMap(Point currentPoint, int zoom, Size size){return null;}

        protected void WriteToCache(string path,byte[] arrayByte)
        {
            MemoryStream memStream = new MemoryStream(arrayByte);
            Bitmap bit = new Bitmap(memStream);
            bit.Save(path, System.Drawing.Imaging.ImageFormat.Png);
            bit.Dispose();
            memStream.Close();
        }

        protected byte[] ReadFromCache(string path)
        {
            Bitmap bit = new Bitmap(path);
            MemoryStream memStream = new MemoryStream();
            bit.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteResult = memStream.ToArray();
            memStream.Close();
            bit.Dispose();
            return byteResult;
        }

        protected byte[] ImageToByte(Image image)
        {
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] byteResult = ms.ToArray();
            ms.Close();
            image.Dispose();
            return byteResult;

        }
    }
}
